/**
 * Created by huachuan on 2016/1/13.
 */
angular.module('userhomeservice',[])
  //约局的Service
  .factory('UserHome', function($resource,$rootScope,ENV) {

    //访问的地址
    var APIUrl = ENV.api;
    //接收返回值
    var memberInfoJson,attendMemberListJson,
      userHomeListJson,pageSize = 10,
      userMeetingListJson;
    //获取$resource
    var getOtherUserInfoResource = $resource(APIUrl + 'myhome/go2OtherHome.do');
    var getUserAttendMemberListResource = $resource(APIUrl + 'myhome/getFocusFriendList.do');
    var getUserHomeListResource = $resource(APIUrl + 'myhome/getHomeMessageList.do');
    var getUserMeetingListResource = $resource(APIUrl + 'myhome/getMeetingList.do');
    return {
        //获取该会员发布的约局
        getUserMeetingList:function(memberId,tolookMemberId){
          var hasNextPage = true;
          getUserMeetingListResource.save({
            member_id:memberId,
            look_member_id:tolookMemberId,
            pageIndex:1,
            pageSize:pageSize
          },function(response){
            if(response.meetingList.length<pageSize){
              hasNextPage = false;
            }
            userMeetingListJson ={
              hasNextPage:hasNextPage,
              nextPage:2,
              data:response.meetingList
            };
            $rootScope.$broadcast('UserHome.userMeetingListUpdated');
          });
        },
        //获取更多会员的约局
      getMoreUserMeetingList:function(memberId,tolookMemberId){
        if(userMeetingListJson===undefined){
          return false;
        }
        var hasNextPage=userMeetingListJson.hasNextPage;
        var nextPage=userMeetingListJson.nextPage;
        var moreHomeData=userMeetingListJson.data;
        if(!hasNextPage){
          return false;
        }
        getUserMeetingListResource.save({
          member_id:memberId,
          look_member_id:tolookMemberId,
          pageIndex:nextPage,
          pageSize:pageSize
        },function(response){
          nextPage++;
          if(response.meetingList.length<pageSize){
            hasNextPage = false;
          }
          moreHomeData = moreHomeData.concat(response.meetingList);
          userMeetingListJson = {
            hasNextPage:hasNextPage,
            nextPage: nextPage,
            data: moreHomeData
          };
          $rootScope.$broadcast('UserHome.userMeetingListUpdated');
        });
      },
        getUserMeetingListJson:function(){
          return userMeetingListJson.data;
        },
      //用户约局是否有下一页
      meetingHasNextPage:function(){
        if(userMeetingListJson===undefined){
          return false;
        }
        return userMeetingListJson.hasNextPage;
      },
        //获取该会员的酒窝
        getUserHomeList:function(memberId,tolookMemberId){
          var hasNextPage = true;
          getUserHomeListResource.save({
            member_id:memberId,
            look_member_id:tolookMemberId,
            pageIndex:1,
            pageSize:pageSize
          },function(response){
            if(response.homeList.length<pageSize){
              hasNextPage = false;
            }
            userHomeListJson ={
              hasNextPage:hasNextPage,
              nextPage:2,
              data:response.homeList
            };
            $rootScope.$broadcast('UserHome.userHomeListUpdated');
          });
        },
      //获取更多会员酒窝的信息
      getMoreUserHomeList:function(memberId,tolookMemberId){
        if(userHomeListJson===undefined){
          return false;
        }
        var hasNextPage=userHomeListJson.hasNextPage;
        var nextPage=userHomeListJson.nextPage;
        var moreHomeData=userHomeListJson.data;
        if(!hasNextPage){
          return false;
        }
        getUserHomeListResource.save({
          member_id:memberId,
          look_member_id:tolookMemberId,
          pageIndex:nextPage,
          pageSize:pageSize
        },function(response){
          nextPage++;
          if(response.homeList.length<pageSize){
            hasNextPage = false;
          }
          moreHomeData = moreHomeData.concat(response.homeList);
          userHomeListJson = {
            hasNextPage:hasNextPage,
            nextPage: nextPage,
            data: moreHomeData
          };
          $rootScope.$broadcast('UserHome.userHomeListUpdated');
        });
      },
      //是否有下一页
      homeHasNextPage:function(){
        if(userHomeListJson===undefined){
          return false;
        }
        return userHomeListJson.hasNextPage;
      },
        getUserHomeListJson:function(){
            return userHomeListJson.data;
        },
        //获取其他会员的关注列表
        getUserAttendList:function(memberId,tolookMemberId){
          getUserAttendMemberListResource.save({
            member_id:memberId,
            look_member_id:tolookMemberId
          },function(response){
            attendMemberListJson = response;
            $rootScope.$broadcast('UserHome.userAttendMemberListUpdated');
          });
        },
        getUserAttendListJson:function(){
          return attendMemberListJson;
        },
        //获取到用户的信息
        getOtherUserInfo:function(memberId,tolookMemberId){
          getOtherUserInfoResource.save({
            member_id:memberId,
            look_member_id:tolookMemberId
          },function(response){
            memberInfoJson = response;
            $rootScope.$broadcast('UserHome.memberInfoUpdated');
          });
        },
        getMemberInfoJson:function(){
          return memberInfoJson;
        }
    }
  });
